//
//  Movie.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/17.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "Movie.h"
#import "MovieModal.h"
#import "MovieCell.h"
#import "common.h"
#import "jsonAnalysis.h"


@interface Movie ()
{
    BOOL isLeft;
    NSMutableArray *_movieModalArray;
}

@end

// hahaah


@implementation Movie

#pragma mark - DefaultOperation

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = @"电影";
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self _loadData];

    [self _createNav];
    [self _createPosterView];
    [self _createMovieTableView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData

- (void)_loadData
{

    NSDictionary *rootDic = [jsonAnalysis initWithJsonFileName:@"us_box.json"];
    
    NSArray *subjects = [rootDic objectForKey:@"subjects"];
    
    _movieModalArray = [[NSMutableArray alloc] init];
    
    for (NSDictionary *dic in subjects)
    {
        NSDictionary *subject = [dic objectForKey:@"subject"];
        MovieModal *modal = [[MovieModal alloc] init];
//        modal.title = [subject objectForKey:@"title"];
//        modal.year = [subject objectForKey:@"year"];
//        modal.images = [subject objectForKey:@"images"];
//        NSNumber *number = [[subject objectForKey:@"rating"] objectForKey:@"average"];
//        modal.average = [number floatValue];
        
        [modal setValuesForKeysWithDictionary:subject];
        
        [_movieModalArray addObject:modal];
    }
    
//    NSLog(@"%@",finalArray);    
    
}

#pragma mark - CreateSubViews

- (void)_createNav
{
    UIView *flipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [button1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [button1 sizeToFit];
    
    [flipView addSubview:button1];
    
    button1.tag = 1;
    
    [button1 addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [button2 sizeToFit];
    
    [flipView addSubview:button2];
    
    button2.tag = 2;
    
    [button2 setHidden:YES];
    
    [button2 addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:flipView];

    self.navigationItem.rightBarButtonItem = item;

}

- (void)_createPosterView
{
    _posterView  = [[UIView alloc] initWithFrame:self.view.bounds];
    
    
    
    
    [self.view addSubview:_posterView];
}

- (void)_createMovieTableView
{
    _movieTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KWidth, KHeight - 49 - 64)];
    [_movieTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    _movieTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _movieTableView.delegate = self;
    _movieTableView.dataSource = self;
    _movieTableView.hidden = YES;
    [self.view addSubview:_movieTableView];
    
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    
    [_movieTableView registerNib:nib forCellReuseIdentifier:@"movieCell"];
}

#pragma mark - MovieTableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_movieModalArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell" forIndexPath:indexPath];
    
    cell.modal = _movieModalArray[indexPath.row];
    
    return cell;
}

#pragma mark - Actions

- (void)buttonAction
{
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    
//    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    
    UIButton *btn1 = (UIButton *)[flipView viewWithTag:1];
    UIButton *btn2 = (UIButton *)[flipView viewWithTag:2];
    
    isLeft = !isLeft;
    
//    [UIView transitionWithView:flipView duration:0.5 options:option animations:nil completion:nil];
    
    [self _flipView:flipView isLeft:isLeft];
    [self _flipView:self.view isLeft:isLeft];
    
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    
    _posterView.hidden = !_posterView.hidden;
    _movieTableView.hidden = !_movieTableView.hidden;
    
}

#pragma mark - Tools

- (void)_flipView:(UIView *)view isLeft:(BOOL)_isLeft
{
    
    UIViewAnimationOptions option = _isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:view duration:0.5 options:option animations:nil completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
